﻿using System.Configuration;
using System.Data.Entity.Validation;
using System.Diagnostics;
using System.Web.Configuration;
using System.Xml;
using M2B.GestionEntretiens.POCOS;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;




namespace M2B.GestionEntretiens.DAL
{
    /// <summary>
    /// Cette Classe décrit le context gestion des entretiens
    /// </summary>
    /// <remarks>
    /// Informations de la classe : 
    /// <list type="bullet">
    /// <item name="authors">Auteur : M2B : Bilel / Maher SAMMARI</item>
    /// <item name="creation date">Date de création : 01/04/2014</item>
    /// <item name="last modification">Date de dernière modification : 01/04/2014</item>
    /// </list>
    /// </remarks>
    public class GestionEntretiensContext : DbContext
    {
        #region Properties
        /// <summary>
        /// Candidats entity
        /// </summary>
        public DbSet<Candidat> Candidats { get; set; }
        /// <summary>
        /// Entretiens entity
        /// </summary>
        public DbSet<Entretien> Entretiens { get; set; }
        /// <summary>
        /// Questions entity
        /// </summary>
        public DbSet<Question> Questions { get; set; }
        /// <summary>
        /// Tests entity
        /// </summary>
        public DbSet<Test> Tests { get; set; }
        /// <summary>
        /// Propositions entity
        /// </summary>
        public DbSet<Proposition> Propositions { get; set; }
        /// <summary>
        /// Referentiels entity
        /// </summary>
        public DbSet<Referentiel> Referentiels { get; set; }
        /// <summary>
        /// Réponses entity
        /// </summary>
        public DbSet<Reponse> Reponses { get; set; }
        #endregion

        #region Constructeurs

        /// <summary>
        /// Constructeur par défaut
        /// </summary>
        public GestionEntretiensContext(string connectionString)
            : base(connectionString)
        {
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<GestionEntretiensContext>());
            Configuration.ProxyCreationEnabled = false;
            Configuration.LazyLoadingEnabled = false;

        }
        #endregion

        #region Méthodes
        //protected override void OnModelCreating(DbModelBuilder modelBuilder)
        //{
        //    modelBuilder.Entity<Candidat>()
        //      .HasKey(t => t.CandidatId);

        //    modelBuilder.Entity<Entretien>()
        //        .HasRequired(t => t.Candidat)
        //        .WithMany(t => t.Entretiens);
        //}
        /// <summary>
        /// Commit du modéle des données
        /// </summary>
        public virtual void Commit()
        {
            try
            {
                base.SaveChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Class: {0}, Property: {1}, Error: {2}",
                            validationErrors.Entry.Entity.GetType().FullName,
                            validationError.PropertyName,
                            validationError.ErrorMessage);
                    }
                }

              
            }
          
        }
        #endregion
    }

   
}
